#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h> 

sem_t unname_sem;
#define max_size 10000
int count = 0;

void *add_num(void *arg){
    sem_wait(&unname_sem);
    count=count+1;
    sem_post(&unname_sem);
}

int main(int argc, char const *argv[])
{
    /**
     * 
     * (sem_t *__sem, int __pshared, unsigned int __value)
     * __pshared: 1表示进程间通讯 0表示线程间通讯
     * __value:信号量的初始值
     */
    if((sem_init(&unname_sem,0,1))==-1){
        perror("sem_init");
        exit(EXIT_FAILURE);
    };

    pthread_t tid[max_size];
    for (int i = 0; i < max_size; i++)
    {
        pthread_create(&tid[i],NULL,add_num,NULL);
    }
    for (int i = 0; i < max_size; i++)
    {
        pthread_join(tid[i],NULL);
    }
    sem_destroy(&unname_sem);
    printf("count = %d\n",count);
    
    return 0;
}
